<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="cardNumber">
        <el-input
          v-model="queryParams.cardNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入电子邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入商家地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <!-- <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.app_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
        <!-- v-hasPermi="['system:application:export']" -->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商家id" align="center" prop="businessId" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="身份证号" align="center" prop="cardNumber" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="电子邮箱" align="center" prop="email" />
      <!-- <el-table-column label="商家地址" align="center" prop="address" /> -->
      <!-- <el-table-column label="主体类型" align="center" prop="businessType" /> -->
      <!-- <el-table-column label="营业执照" align="center" prop="licenseUrl" />
      <el-table-column label="身份证正面" align="center" prop="cardFrontUrl" />
      <el-table-column label="身份证反面" align="center" prop="cardBackUrl" /> -->
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-show="scope.row.state==dict.type.app_state[0].value"
            type="primary"
            @click="handleUpdate(scope.row)"
          >审核</el-button>
          <!-- v-hasPermi="['system:application:edit']" -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商家id" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商家id" disabled/>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" disabled/>
        </el-form-item>
        <el-form-item label="身份证号" prop="cardNumber">
          <el-input v-model="form.cardNumber" placeholder="请输入身份证号" disabled/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" disabled/>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" disabled/>
        </el-form-item>
        <el-form-item label="商家地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入商家地址" disabled/>
        </el-form-item>
        <el-form-item label="主体类型">
          <dict-tag :options="dict.type.business_type" :value="form.businessType" />
        </el-form-item>
        <el-form-item label="营业执照" prop="licenseUrl">
          <!-- <el-input v-model="form.licenseUrl" placeholder="请输入营业执照" /> -->
          <el-image
            style="width: 300px; height: 300px"
            fit="contain"
            :src="imgBaseUrl+form.licenseUrl"
            :preview-src-list="[imgBaseUrl+form.licenseUrl]"
            >
          </el-image>
        </el-form-item>
        <el-form-item label="身份证照片">
          <el-image 
            style="width: 300px; height: 300px;margin-right: 20px;"
            fit="contain"
            :src="imgBaseUrl+form.cardFrontUrl"
            :preview-src-list="[imgBaseUrl+form.cardFrontUrl,imgBaseUrl+form.cardBackUrl]">
          </el-image>
          <el-image
            style="width: 300px; height: 300px"
            fit="contain"
            :src="imgBaseUrl+form.cardBackUrl"
            :preview-src-list="[imgBaseUrl+form.cardBackUrl,imgBaseUrl+form.cardFrontUrl]">
          </el-image>
        </el-form-item>
        <!-- <el-form-item label="身份证反面">
         
        </el-form-item> -->
        <el-form-item label="状态" prop="state">
          <dict-tag :options="dict.type.app_state" :value="form.state" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button type="success" plain @click="setAppState(dict.type.app_state[2])">同 意</el-button>
        <el-button type="danger" plain @click="setAppState(dict.type.app_state[1])">拒 绝</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApplication, getApplication,updateApplication} from "@/api/store/store";

export default {
  name: "Application",
  dicts:["app_state","business_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      applicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        realName: null,
        cardNumber: null,
        phone: null,
        email: null,
        address: null,
        businessType: null,
        licenseUrl: null,
        cardFrontUrl: null,
        cardBackUrl: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      imgBaseUrl: process.env.VUE_APP_BASE_API
    };
  },
  created() {
    console.log(this.dict.type.app_state)
    this.getList();
  },
  methods: {
    meassage(text) {
      this.$prompt(text, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: (value) => { //非空验证
          if (!value) {
            return '内容不能为空'
          }
        },
      }).then(({ value }) => {
        this.form.reason = value
        this.updateForm()
        return true
      }).catch(() => {
        return false
      });
    },
    updateForm(){
      updateApplication(this.form).then(res=>{
            if(res>0){
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            }else{
              this.$modal.msgError("操作失败");
              this.open = false;
            }
          })
    },
    setAppState(val){
      this.form.state = val.value
      if(val.label=="未通过"){
        this.meassage("请输入不通过原因")
      }else{
        this.updateForm()
      }
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listApplication(this.queryParams).then(response => {
        this.applicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        realName: null,
        cardNumber: null,
        phone: null,
        email: null,
        address: null,
        businessType: null,
        licenseUrl: null,
        cardFrontUrl: null,
        cardBackUrl: null,
        state: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApplication(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审核商家【"+row.businessId+"】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApplication(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplication(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delApplication(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('store/application/export', {
        ...this.queryParams
      }, `application_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
